package com.jnevision.deviceapply.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnevision.deviceapply.model.DeviceApply;
import com.jnevision.deviceapply.model.DeviceApplyCondition;
import com.jnevision.deviceapply.model.DeviceApplyVO;
import com.jnevision.deviceapply.service.DeviceApplyServiceImpl;
import com.jnevision.user.model.User;
import com.jnevision.user.service.UserServiceImpl;

/**
 * <p>
 * 设备申请 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-14
 */
@RestController
@RequestMapping("/deviceapplys")
public class DeviceApplyController extends ApiController {
    @Autowired
    private DeviceApplyServiceImpl deviceApplyServiceImpl;

	@Autowired
	private UserServiceImpl userServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody DeviceApply device) {
        return this.success(this.deviceApplyServiceImpl.save(device));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody DeviceApply device) {
        return this.success(this.deviceApplyServiceImpl.updateById(device));
    }

    @GetMapping
    public R<DeviceApplyVO> get(String id) {
	    DeviceApply byId = this.deviceApplyServiceImpl.getById(id);
	    DeviceApplyVO vo = new DeviceApplyVO();
	    BeanUtils.copyProperties(byId, vo);
	    if (StringUtils.isNotEmpty(vo.getUserId())) {
	    	User byId2 = this.userServiceImpl.getById(vo.getUserId());
	    	if (null != byId2) {
	    		vo.setUserName(byId2.getName());
	    		vo.setDeptId(byId2.getDeptId());
	    		vo.setPhone(byId2.getPhone());
	    	}
	    }
	    
        return this.success(vo);
    }

    /**
     * 根据借物品的时间范围查询
     * 
     * @param page
     * @param dc
     * @return
     */
    @GetMapping("/page")
    public R<IPage<DeviceApplyVO>> page(Page<DeviceApplyVO> page, DeviceApplyCondition dc) {
        return this.success(this.deviceApplyServiceImpl.getPage(page, dc));
    }
}
